package com.company.site.vote;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.company.site.mode.vote.Lottery;
import com.company.site.mode.vote.Player;
import com.company.site.mode.vote.Statistics;
import com.company.site.mode.vote.Vote;

@Service
public class VoteService {
	
	@Resource
	VoteMapper voteMapper;

	public List<Player> findPlayerInfo(Player player) {
		return voteMapper.findPlayerInfo(player);
	}

	public void addPlayerVotedNum(Long id) {
		voteMapper.addPlayerVotedNum(id);
	}

	public void addVote(Vote vote) {
		voteMapper.addVote(vote);
	}

	public Statistics findStatistics() {
		return voteMapper.findStatistics();
	}

	public void addTotalVisted() {
		voteMapper.addTotalVisted();
	}

	public void addTotalVoted() {
		voteMapper.addTotalVoted();
	}

	public List<Player> loadVote(Integer page) {
		return voteMapper.loadVote(page);
	}

	public Player findPlayerById(Long id) {
		return voteMapper.findPlayerById(id);
	}

	public void addPlayer(Player player) {
		voteMapper.addPlayer(player);
	}

	public List<Player> findRankPlayer() {
		return voteMapper.findRankPlayer();
	}

	public void addTotalPlayer() {
		voteMapper.addTotalPlayer();
	}

	public List<Vote> findTodayVoteListByOpenid(String openid) {
		return voteMapper.findTodayVoteListByOpenid(openid);
	}

	public void updatePlayerC(Player player) {
		voteMapper.updatePlayerC(player);
	}

	public void cheatVote(List<Vote> list) {
		voteMapper.cheatVote(list);
	}

	public void cheatTotalVoted(Map<String, Object> map) {
		voteMapper.cheatTotalVoted(map);
	}

	public void cheatPlayerVotedNum(Map<String, Object> map) {
		voteMapper.cheatPlayerVotedNum(map);
	}

	public List<Vote> findTodayVoteListByOpenidAndSamePlayer(Vote voteSearch) {
		return voteMapper.findTodayVoteListByOpenidAndSamePlayer(voteSearch);
	}

	public Vote getVoteByOpenid(String openid) {
		return voteMapper.getVoteByOpenid(openid);
	}

	public Integer addLottery(Lottery lottery) {
		return voteMapper.addLottery(lottery);
	}
	
	public static Integer getLotteryResult() {
		
		Integer result = 0;
		//获取随机数
//		result = (int) (Math.random() * VoteConstant.LOTTERY_CONTENT_ARR.length);//0-7
		
		//权重获取随机数
		Integer totalWeight = 0;
		Integer length = VoteConstant.LOTTERY_CONTENT_ARR.length;
		Integer[] randomRangeArr = new Integer[length];
		Integer random = 0;
		for (int i = 0; i < length; i++) {
			totalWeight = totalWeight + VoteConstant.LOTTERY_WEIGHT_ARR[i];
			randomRangeArr[i] = totalWeight.intValue();//不可为引用类型
		}
		random=(int) (Math.random() * totalWeight);
		for (int i = 0; i < length; i++) {
			Integer min = ((i-1)>=0)?randomRangeArr[i-1]:0;
			Integer max = randomRangeArr[i];
			if (random>min
				&&random<=max) {
				return i;
			}
		}
		
		// 若循环中无法获取到结果，则结果取0
		return result;
	}

	public Lottery getLotteryById(Long id) {
		return voteMapper.getLotteryById(id);
	}

	public void updateLotteryMobile(Lottery lottery) {
		voteMapper.updateLotteryMobile(lottery);
	}

	public void updateVoteLotteryStatus(Long id) {
		voteMapper.updateVoteLotteryStatus(id);
		
	}

}
